/*
 * By: Werner Schuster (murphee)
 * 
 * Copyright (c) 2005-2006 Werner Schuster (murphee)
 * 
 * This file is part of the EclipseShell project. EclipseShell is subject 
 * to the Eclipse Public License (EPL) available at
 * http://www.eclipse.org/org/documents/epl-v10.php
 * or in the file /epl-v10.html in the source for other info
 * see the /eclipseshell.license file
 */
package net.sf.eclipseshell.internal.actions;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import net.sf.eclipseshell.EclipseShellPlugin;
import net.sf.eclipseshell.extensions.IExecutionEnvironmentProvider;
import net.sf.eclipseshell.internal.extensions.ExecutionProviderManager;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class PlaceholderAction implements IObjectActionDelegate {

	private Object selection_;

	/**
	 * Constructor for Action1.
	 */
	public PlaceholderAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
	    if (selection_ instanceof IFile) {
			IFile file = (IFile) selection_;
			FileReader reader = null;
			try {
				MessageConsoleStream msgStream = getMessageConsole();
				
				reader = new FileReader(file.getRawLocation().toFile());
				
				String extension;
				int extensionIndex = file.getFullPath().toOSString().lastIndexOf('.');
				if(extensionIndex > 0){
					extension = file.getFullPath().toOSString().substring(extensionIndex+1);
					IExecutionEnvironmentProvider prov = ExecutionProviderManager.getProvider(extension);
					if(prov != null){
						msgStream.write(">"+prov.createNew(file, msgStream, "foo").execute(reader));
					} else {
						msgStream.write("No Execution Environment for "+ extension + "found");
					}


				}else {
					msgStream.write("No file extension -> No Interpreter.");
				}

			} catch (FileNotFoundException e) {
				EclipseShellPlugin.getDefault().log(Status.WARNING, e.getLocalizedMessage(), e);
			} catch (IOException e) {
				EclipseShellPlugin.getDefault().log(Status.WARNING, e.getLocalizedMessage(), e);
			} finally {
				try {
					reader.close();
				} catch (IOException e) {
					EclipseShellPlugin.getDefault().log(Status.WARNING, e.getLocalizedMessage(), e);
				}
			}
		}
	}

	
	private MessageConsoleStream getMessageConsole() {
		MessageConsole msgConsole = new MessageConsole(
				"Beanshell output", null);
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(
				new IConsole[] { msgConsole });
		ConsolePlugin.getDefault().getConsoleManager().showConsoleView(
				msgConsole);
		MessageConsoleStream msgStream = msgConsole.newMessageStream();
		return msgStream;
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		if(selection.isEmpty()){
			selection_ = null;
		}
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structSel = (IStructuredSelection) selection;
			selection_ = structSel.getFirstElement();
		}
	}

}
